package com.gdou.controller;

import cn.hutool.core.util.StrUtil;
import com.gdou.commons.Result;
import com.gdou.model.SysNotice;
import com.gdou.service.SysNoticeService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

@RestController
@RequestMapping("/notice")
public class SysNoticeController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SysNoticeService sysNoticeService;



    @GetMapping("list")
    public Result list(String title){
//        Page<SysNotice> pageData = sysNoticeService.page(getPage(),new LambdaQueryWrapper<SysNotice>()
//                .like(StrUtil.isNotBlank(title),SysNotice::getTitle,title).orderByDesc(SysNotice::getCreated));
//        return Result.ok().data("pageData",pageData);
        Result result = sysNoticeService.listNOtice(title, request);
        return result;
    }



    @PostMapping("save")
    public Result save(@RequestBody SysNotice sysNotice){
        sysNotice.setCreated(new Date());

        return sysNoticeService.save(sysNotice);
    }

    @GetMapping("info/{id}")
    public Result info(@PathVariable("id") Long id){

        return sysNoticeService.getById(id);
    }

    @PostMapping("update")
    public Result update( @RequestBody SysNotice sysNotice){
        sysNotice.setUpdated(new Date());

        return sysNoticeService.updateById(sysNotice);
    }

    @PostMapping("delete")
    public Result delete(@RequestBody Long[] noticeIds){

        return sysNoticeService.removeByIds(Arrays.asList(noticeIds));
    }
}